package servlet;

import domain.Product;
import service.ProductService;
import service.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by guo on 2017/10/24.
 */
@WebServlet(name = "UpdateServlet",urlPatterns = "/updateServlet")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pid=Integer.valueOf(request.getParameter("pid"));
        String pname=request.getParameter("name");
        Double market_price=Double.valueOf(request.getParameter("mprice"));
        Double shop_price=Double.valueOf(request.getParameter("sprice"));
        String pdesc=request.getParameter("desc");
        ProductService productService=new ProductServiceImpl();
        Boolean ps= null;
        Product pd=new Product();
        try {
            ps = productService.Update(pname,market_price,shop_price,pdesc,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(ps==true) {
           pd.setPname(pname);
            pd.setMarket_price(market_price);
            pd.setShop_price(shop_price);
            pd.setPdesc(pdesc);
            request.setAttribute("pd",pd);
            request.getRequestDispatcher("/showList.jsp").forward(request,response);
        }
    }
    }

